﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using BoolanSlide.RedoUndo;
using Windows.UI;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;

namespace BoolanElement
{
    public class BooRoundRectElement:BooTextShapesBaseElement
    {

        public double RadiusX { get; set; }
        public double RadiusY { get; set; }

        public BooRoundRectElement()
        { 
        }

        public override async Task<BooUIBase> Render(SaveOriginalElement saveOriginal, SaveForRedoUndo saveForRedoUndo)
        {
            BooRoundedRectangle roundedRect = new BooRoundedRectangle(this, saveForRedoUndo);
            RedoUndoHelper.SaveOriginalStatus(roundedRect);
            return roundedRect;
        }
        public override async Task<BooUIBase> RenderForThumbNails()
        {
            BooRoundedRectangle roundedRect = new BooRoundedRectangle(this);
            return roundedRect;
        }

        public override XElement SaveToXElement()
        {
            XElement element = new XElement(typeof(BooRoundRectElement).Name);
            element.SetAttributeValue("UniqueID", UniqueID.ToString());
            element.Add(ElementTransform.Save());
            XElement styleElement = SaveShapeStyle();
           
            //save radius
            styleElement.Add(SaveRadius());
            element.Add(styleElement);
            element.Add(SaveData());

            return element;
        }

        public static BooRoundRectElement InitElementStyle()
        {
            BooRoundRectElement roundRectElement = new BooRoundRectElement();
            roundRectElement.ElementTransform.Rotate.Angle = 0;
            roundRectElement.ElementTransform.Rotate.CenterX = 0;
            roundRectElement.ElementTransform.Rotate.CenterY = 0;

            roundRectElement.ElementTransform.Scale.ScaleX = 1;
            roundRectElement.ElementTransform.Scale.ScaleY = 1;
            roundRectElement.ElementTransform.Scale.CenterX = 0;
            roundRectElement.ElementTransform.Scale.CenterY = 0;

            roundRectElement.ElementTransform.Translate.X = 0;
            roundRectElement.ElementTransform.Translate.Y = 0;

            roundRectElement.ElementTransform.Margin = new Windows.UI.Xaml.Thickness(0, 0, 0, 0);

            roundRectElement.ElementTransform.Width = 200;
            roundRectElement.ElementTransform.Height = 200;

            roundRectElement.TextMarginLeft = 0;
            roundRectElement.TextMarginTop = 0;
            roundRectElement.TextMaxHeight = 200;
            roundRectElement.TextWidth = 200;
           
            roundRectElement.TextAlignment = SerializeCast.StringToTextAlignment("CenterCenter");

            roundRectElement.ShapeStyle = SerializeCast.StringToTextBoxStyle("NoBorderBKG");
            roundRectElement.BorderColor = Color.FromArgb(255, 0, 0, 0);
            roundRectElement.BorderWidth = 0;

            roundRectElement.ShapeFillColor = Color.FromArgb(255, 24, 122, 169);
            roundRectElement.ShapeWidth = 200;
            roundRectElement.ShapeHeight = 200;

            roundRectElement.ControlPanelTop = 0;
            roundRectElement.ControlPanelLeft = 0;

            roundRectElement.RadiusX = 20;
            roundRectElement.RadiusY = 20;

            roundRectElement.Text = "";

            return roundRectElement;
        }

        public static BooRoundRectElement InitElementFromXml(XElement element)
        {
            BooRoundRectElement rectElement = new BooRoundRectElement();
            rectElement.LoadFromXElement(element);

            return rectElement;
        }

        public XElement SaveRadius()
        {
            XElement radius = new XElement("Radius");
            radius.SetAttributeValue("X", RadiusX);
            radius.SetAttributeValue("Y", RadiusY);

            return radius;
        }

        public void LoadRadius(XElement element)
        {
            var radius = element.Element("Radius");
            this.RadiusX = Double.Parse(radius.Attribute("X").Value);
            this.RadiusY = Double.Parse(radius.Attribute("Y").Value);
        }

        public override void LoadFromXElement(XElement element)
        {
            XElement tranformElement = element.Element("Transform");
            XElement dataElement = element.Element("Data");
            XElement styleElement = element.Element("Style");

            ElementTransform.Load(tranformElement);
            LoadShapeStyle(styleElement);
            LoadRadius(styleElement);
            LoadData(dataElement);
        }

        public override string ToString()
        {
            throw new NotImplementedException();
        }


        public override BooElement Clone()
        {
            BooRoundRectElement element = new BooRoundRectElement();
            element.ElementTransform = this.ElementTransform.Clone();

            element.TextMarginLeft = this.TextMarginLeft;
            element.TextMarginTop = this.TextMarginTop;
            element.TextMaxHeight = this.TextMaxHeight;
            element.TextWidth = this.TextWidth;
        
            element.TextAlignment = this.TextAlignment;

            element.ShapeStyle = this.ShapeStyle;
            element.BorderColor = Color.FromArgb(this.BorderColor.A, this.BorderColor.R, this.BorderColor.G, this.BorderColor.B);
            element.BorderWidth = this.BorderWidth;

            element.ShapeFillColor = Color.FromArgb(this.ShapeFillColor.A, this.ShapeFillColor.R, this.ShapeFillColor.G, this.ShapeFillColor.B);
            element.ShapeWidth = this.ShapeWidth;
            element.ShapeHeight = this.ShapeHeight;

            element.ControlPanelTop = this.ControlPanelTop;
            element.ControlPanelLeft = this.ControlPanelLeft;
            element.Text = this.Text;

            element.RadiusX = this.RadiusX;
            element.RadiusY = this.RadiusY;

            return element;

        }

        public override void Load(BooTextShapesBaseElement shapesElement)
        {
            BooRoundRectElement ele = shapesElement as BooRoundRectElement;
            this.ElementTransform = ele.ElementTransform;

            this.TextMarginLeft = ele.TextMarginLeft;
            this.TextMarginTop = ele.TextMarginTop;
            this.TextMaxHeight = ele.TextMaxHeight;
            this.TextWidth = ele.TextWidth;

            this.TextAlignment = ele.TextAlignment;

            this.ShapeStyle = ele.ShapeStyle;
            this.BorderColor = Color.FromArgb(ele.BorderColor.A, ele.BorderColor.R, ele.BorderColor.G, ele.BorderColor.B);
            this.BorderWidth = ele.BorderWidth;

            this.ShapeFillColor = Color.FromArgb(ele.ShapeFillColor.A, ele.ShapeFillColor.R, ele.ShapeFillColor.G, ele.ShapeFillColor.B);
            this.ShapeWidth = ele.ShapeWidth;
            this.ShapeHeight = ele.ShapeHeight;

            this.ControlPanelTop = ele.ControlPanelTop;
            this.ControlPanelLeft = ele.ControlPanelLeft;
            this.Text = ele.Text;

            this.RadiusX = ele.RadiusX;
            this.RadiusY = ele.RadiusY;
        }
    }
}
